<template>
  <div class="container">
    <!-- el卡片组件 -->
    <el-card>
      <el-form
        ref="inputLeft"
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
      >
        <!-- 左侧input框 --关键字 -->
        <el-form-item prop="user" label="关键字">
          <el-input
            v-model="formInline.user"
            placeholder="根据文章标题搜索"
            @clear="clear"
          ></el-input>
        </el-form-item>
        <!-- 中间input框 --状态 -->
        <el-form-item prop="region" label="状态">
          <el-select v-model="formInline.region" placeholder="请选择">
            <el-option label="启用" value="启用"></el-option>
            <el-option label="禁用" value="禁用"></el-option>
          </el-select>
        </el-form-item>
        <!-- 右侧按钮 -->
        <el-form-item>
          <el-button size="small" plain @click="clear">清除</el-button>
          <el-button size="small" type="primary" @click="find">搜索</el-button>
          <el-button class="btn" type="success">
            <i class="el-icon-edit"></i>
            新增技巧</el-button
          >
        </el-form-item>
      </el-form>
      <!-- 提示信息 -->
      <el-alert title="数据一共 17 条" type="info" show-icon :closable="false">
      </el-alert>
      <!-- el-table组件 -->
      <el-table :data="tableHead" style="width: 100%" >
        <el-table-column fixed type="index" label="序号" width="80">
        </el-table-column>
        <el-table-column prop="title" label="文章标题" width="320">
        </el-table-column>
        <el-table-column prop="visits" label="阅读数" width="100">
        </el-table-column>
        <el-table-column prop="username" label="录入人" width="100">
        </el-table-column>
        <el-table-column prop="createTime" label="录入时间" width="100">
        </el-table-column>
        <el-table-column prop="state" label="状态" width="100">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="160">
          <template slot-scope="scope">
            <el-button
              @click.native.prevent="deleteRow(scope.$index, tableData)"
              type="text"
              size="small"
            >
              预览
            </el-button>
            <el-button
              @click.native.prevent="deleteRow(scope.$index, tableData)"
              type="text"
              size="small"
            >
              启用
            </el-button>
            <el-button
              @click.native.prevent="deleteRow(scope.$index, tableData)"
              type="text"
              size="small"
            >
              修改
            </el-button>
            <el-button
              @click.native.prevent="deleteRow(scope.$index, tableData)"
              type="text"
              size="small"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pagesize"
        layout=" prev, pager, next,sizes, jumper"
        :total="24"
      >
      </el-pagination>

    </el-card>
  </div>
</template>

<script>
import { list } from '@/api/hmmm/articles'
export default {
  data () {
    return {
      page: 1, // 页码
      pagesize: 5, // 每页条数
      dialogVisible: false,
      currentPage4: 1,
      formInline: {
        user: '',
        region: ''
      },
      // 表单数据
      tableHead: []
    }
  },
  created () {
    this.getList()
    // const bb = this.aaa()
    // const cc = this.aaa()
    // bb.a = 789
    // cc.a = 456
    // console.log(bb)
    // console.log(cc)
  },
  methods: {
    // 获取列表
    async getList () {
      const { data: { items } } = await list({ pagesize: 5 })
      // console.log(items)
      // console.log(items[0])
      // items.forEach(item => {
      //   // console.log(item.creatorID)
      //   // console.log(item.visits)
      // })
      this.tableHead = items
    },
    // aaa () {
    //   console.log(123)
    //   return { a: 33 }
    // },
    async find () {
      console.log('搜索文章')
      console.log(this.formInline.user, this.formInline.region)
      const { data: { items } } = await list()
      // console.log(items
      // 根据条件过滤
      const array = items.filter(item => item.title === this.formInline.user)
      console.log('过滤的title相等的数据', array)
    },
    // 清除from表单数据·
    clear () {
      console.log('清除')
      this.$refs.inputLeft.resetFields()
    },
    async handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.page = 1
      const { data: { items } } = await list({ pagesize: this.pagesize })
      // console.log(items)
      this.tableHead = items
    },
    async handleCurrentChange (val) {
      this.page = val
      console.log(`当前页: ${val}`)
      const { data: { items } } = await list({ pagesize: this.pagesize, page: this.page })
      console.log(items)
      this.tableHead = items
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep .el-card {
  margin: 20px;
}
</style>
